<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Chips extends Admin_Controller {
	
	function __construct(){
		parent::__construct();
		define('MODELNAME', 'chips') ;		//模型名
		$this->load->_model('model_chips','chips' ,TRUE); //载入产品管理模型
		$this->load->library('pagination'); //载入分页类
	}
	
	public function index()
	{
		$this->_chips_info();
	}
	
	
	/**
	 * 
	 * 列表
	 */
	public function _chips_info(){
		
		 
		if(isset($_POST['submit']))
		{
			
			$post = $this->input->post();

			foreach ($post['chipname'] as $k => $v) {
                if($v)
                {
                    $update[$k]['data'] = array('chipname' => $v );
                }
                else
                {
                    $update[$k]['data'] = 'del';
                }
			}
			foreach ($post['content'] as $k => $v) {
                if($v)
                {
                    $update[$k]['data'] += array('content' => $v );
                }
                else
                {
                    $update[$k]['data'] = 'del';
                }
			}


			//循环更新写入chips数据
			foreach (array_keys($update) as $value) {
                if($update[$value]['data'] == 'del')
                {
                    $this->chips->remove($value);
                }
                else
                {
                    $this->chips->save($update[$value]['data'],$value);
                }

			}
			
			
			//插入新的chips数据
			if($post['new_chipname']){
				$this->chips->insert(array('chipname'=>$post['new_chipname'],'content'=>$post['new_content']));
			}
			
			
			$this->_message("保存碎片成功",'chips', TRUE );
			
		}
		else 
		{
			
			//取得信息列表
			$data['list'] = $this->chips->find_all();
			$this->_template('chips',$data);
			
		}
		
		
		
	}
	
	
	 

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 
	 
	
	 
}
